<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<!-- head 头部 -->
<head th:replace="admin/layout/include::head('支付码管理','')"></head>

<body>
<!-- loading 动画-->
<div th:replace="admin/layout/include::loading"></div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="pull-left layui-form-pane search-box">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input id="edtSearch" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
                        <!--<button id="btnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>-->
                    </div>
                </div>
            </div>

            <!-- 数据表格 -->
            <table class="layui-table" id="data-table" lay-filter="data-table"></table>
        </div>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="data-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-sm" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-sm" ajax-event="del" ajax-url="/admin/goodsQr/" value="{{d.id}}">删除</a>
</script>
<script type="text/html" id="toolbarOepration">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add"><i
                class="layui-icon">&#xe654;</i>添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" ajax-event="batchDel" ajax-url="/admin/goodsQr/">
            <i class="layui-icon layui-icon-delete"></i>批量删除</button>
    </div>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="data-table-state">
    <input type="checkbox" lay-filter="data-table-state" value="{{d.id}}" lay-skin="switch" lay-text="正常|禁用"
           {{d.state==0?'checked':''}}/>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="modelGoodsQr">
    <form id="modelGoodsQrForm" lay-filter="modelGoodsQrForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label required">支付码</label>
            <div class="layui-input-block">
                <input name="payNumber" placeholder="请输入支付码" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">二维码地址</label>
            <div class="layui-input-block">
                <textarea name="imgUrl" placeholder="请输入二维码" class="layui-textarea" maxlength="100"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">价格</label>
            <div class="layui-input-block">
                <input name="price" placeholder="请输入价钱，带小数点后面" type="number" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>

        <!--<div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                <input type="radio" name="type" value="男" title="男" checked/>
                <input type="radio" name="type" value="女" title="女"/>
            </div>
        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label required">类型</label>
            <div class="layui-input-block">
                <select name="type" lay-verType="tips" lay-verify="required">
                    <option value="">请选择类型</option>
                    <option value="0">支付宝</option>
                    <option value="1">微信</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
            <button class="layui-btn" lay-filter="submit" lay-submit>保存</button>
        </div>
    </form>
</script>


</body>


<!--类型-->
<!-- js部分 -->
<div th:replace="admin/layout/include::js"></div>
<script>
    layui.use(['form', 'table', 'util', 'admin'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var layer = layui.layer;
        var formUrl;  // 表单提交的action

        //渲染表格
        var tableIns = table.render({
            elem: '#data-table',
            url: '/admin/goodsQr/',
            page : true,
            toolbar: '#toolbarOepration',
			loading : true,
			height : 'full-115',
			limit : 10,
			limits : [ 10, 20, 50, 100 ],
            response:{
	        	countName: 'total'
	        },
            cols: [
                [
                {type:'checkbox'},
                {field: 'id', title: '#',width:40},
                {field: 'payNumber', title: '支付编码',width:130},
                {field: 'price', title: '价钱',sort:true,width:60},
                {field: 'imgUrl',title:'二维码',width:320},
                {field: 'payingCount',title:'正在支付',width:100,templet:function(d){
                        return (d.payingCount==null || d.payingCount==0)?'空闲':'正在支付';
                }},
                {field: 'type', title: '类型',width:80,templet: function (d) {
                        return d.type==0?'支付宝':d.type==1?'微信':'其他';
                }},
                {field: 'state', title: '状态',width:100,templet: '#data-table-state'},
                {
                    field: 'createTime',width:160, sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {align: 'center',width:200, toolbar: '#data-table-bar', title: '操作'}
            ]
            ]
        });

        // 添加按钮点击事件
        $('#btnAdd').click(function () {
            showEditModel();
        });
        //头工具栏事件
        table.on('toolbar(data-table)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    showEditModel();
                    break;
            };
        });

        // 工具条点击事件
        table.on('tool(data-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { //删除
                doDelete(obj);
            }
        });

        // 搜索按钮点击事件
        $('#btnSearch').click(function () {
            var keyword = $('#edtSearch').val();
            tableIns.reload( {where: {keyword: keyword}},'data');
        });

        function doDelete(obj){
            top.layer.confirm('确定要删除吗？',{
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                top.layer.close(i);
                B.del({
                    url:'/admin/goodsQr/'+obj.data.id,
                    loading:true,
                    success:function(data){
                        if (data.success) {
                            layer.msg(data.msg, {icon: 1});
                            obj.del();
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    }
                });
            });
        }
        // 显示表单弹窗
        function showEditModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '支付码',
                content: $('#modelGoodsQr').html(),
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    var url = '/admin/goodsQr/';
                    // 回显数据
                    form.val('modelGoodsQrForm', mData);
                    // 表单提交事件
                    form.on('submit(submit)', function (data) {
                        layer.load(2);
                        // 表单提交事件
                        B.post({
                            url: '/admin/goodsQr/',
                            loading: true,
                            data: data.field,
                            success: function (res) {
                                layer.closeAll('loading');
                                if (res.success) {
                                    layer.close(dIndex);
                                    layer.msg(res.msg, {icon: 1});
                                    tableIns.reload({}, 'data');
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        });
                        return false;
                    });
                }
            });
        }
        // 显示编辑弹窗
        var showEditModel1 = function (data) {
            admin.putTempData("t_qr",data);
            admin.putTempData("formOk",false);
            var index = layer.open({
                type: 2,
                title: data ? '修改页面' : '添加页面',
                area: ['100%', '100%'],
                content: '/admin/sitePage/save',
                success : function(layero, index){
                    //var body = layui.layer.getChildFrame('body', index);
                    setTimeout(function(){
                        layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500)
                },
                end:function () {
                    admin.getTempData('formOk') && tableIns.reload({},'data');  // 成功刷新表格
                }
            });
            layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layer.full(index);
            })

        };

        // 修改状态
        form.on('switch(data-table-state)', function (obj) {
            B.post({
                url: '/admin/goodsQr/updateState',
                loading: true,
                data: {id: obj.elem.value, state: obj.elem.checked ? 0 : 1},
                success: function (data) {
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                    } else {
                        layer.msg(data.msg, {icon: 2});
                        $(obj.elem).prop('checked', !obj.elem.checked);
                        form.render('checkbox');
                    }
                }

            });

        });

    });

</script>
</html>